package bug
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import bug.buffs.BuffData;
	
	//import utils.pool.ObjectPool;
	
	public class Castle  extends Sprite
	{
		internal var _nexts:Vector.<Castle> = new Vector.<Castle>();
		internal var growSpeed:int=5;
		private var _antCount:int=20;
		internal var _power:int;
		private var _display:DisplayObject;
		private var _scene:Scene;
		private var _timer:Timer;
		
		internal var _creating:Boolean=false;
		private var toTarget:Castle;
		private var least:int=5;//最少兵数。
		internal var _isAttack:Boolean;//是否招到攻击
		private var spawn:mc_spawn_note = new mc_spawn_note();//显示数量
		private static var flagC:Dictionary=new Dictionary();//[mc_flag_team_01,mc_flag_team_02,mc_flag_team_03,mc_flag_team_04];
		private var _flagName:String;
		internal var _securityLevel:int;
		private var _flag:DisplayObject;
		private var _isNeutral:Boolean;
		private var attackStateCount:int=2;
		private var autoGrow:Boolean=true;
		internal var _speed:Number=1.6;
		private var _buff:BuffData;
		
		public function Castle(display:DisplayObject,scene:Scene,power:int,autoGrow:Boolean=true,initCount:int=15,isNeutral:Boolean=false)
		{
			flagC[PowerEnum.ME] = mc_flag_team_01;
			flagC[PowerEnum.NEUTRAL]=mc_flag_team_02;
			flagC[PowerEnum.HOSTILITY] =  mc_flag_team_03;
			
			_scene = scene;
			_display=display;
			this.autoGrow=autoGrow;
			antCount=initCount;
			_isNeutral=isNeutral;
			_display.parent.addChild(this);
			addChild(_display);
			this.x=_display.x;
			this.y=_display.y;
			_display.x=_display.y=0;
			this.mouseChildren=false;
			_timer = new Timer(300);
			_timer.addEventListener(TimerEvent.TIMER,onTimer);
			_timer.start();	
			addChild(spawn);
			_flagName=_display.name;
			setPower(power);
		}
		
		public function setBuff(buff:BuffData):void{
			if(_buff){
				if(_buff.id==buff.id) return;
			    _buff.add(buff);
			}else{	
		    	_buff=buff;
			}
			
			growSpeed+=_buff.growSpeed;
			_speed+=_buff.speed;
			antCount+=_buff.antCount;
			updataBuff();
		}
		
		public function get antCount():int
		{
			return _antCount;
		}

		public function set antCount(value:int):void
		{
			if(value<=0) value=0;
			_antCount = value;
			spawn.txcount.text=value.toString();
			addChild(spawn);
		}

		public function stopAction():void{
			_creating=false;
			toTarget=null ;
		}

		public function get isNeutral():Boolean
		{
			return _isNeutral;
		}

		public function get flagName():String
		{
			return _flagName;
		}

		private function setPower(power:int):void{
			if(_power==power) return;
			_power=power;
			if(_flag && _flag.parent){
				_flag.parent.removeChild(_flag);
			}
			var c:Class = flagC[power];//todo:待优化。
			_flag = new c();
			addChild(_flag);
		}
		
		//邻敌人(包括中立)
		public function getEnemyCount():int{
			var r:int;
			for (var j:int = 0; j <_nexts.length; j++) 
			{
				if(_nexts[j]._power!=_power){
					r++;
				}
			}
			return r;
		}
		
		//邻攻击性敌人(不包括中立)
		public function getAttackEnemyCount():int{
			var r:int;
			for (var j:int = 0; j <_nexts.length; j++) 
			{
				if(_nexts[j]._power!=_power && !_nexts[j].isNeutral){
					r++;
				}
			}
			return r;
		}
		
		public function setSecurityLevel(level:int):void{
		    _securityLevel = level;
		}
		
		private function updataBuff():void{
			if(_buff==null) return;
			
			_buff.updata();
			if(_buff.isTimeOut){
				growSpeed-=_buff.growSpeed;
				_speed-=_buff.speed;
				_buff=null;
			}
		}
		
		private function onTimer(e:TimerEvent):void{
			updataBuff();
			createAnt();
			if(autoGrow && _timer.currentCount % growSpeed==0){
				antCount++;
				//setCount(antCount);
			}
			if(_isAttack){
				attackStateCount--;
				if(attackStateCount<=0){
					_isAttack=false;
					attackStateCount=2;
				}
			}
		}
		
		public function stop():void{
			_creating=false;
			toTarget=null;
		}
		
		public function dispose():void{
			_timer.removeEventListener(TimerEvent.TIMER,onTimer);
			_timer.stop();
			_timer=null;
		}
		
		public function setNext(...arg):void{
			for (var i:int = 0; i < arg.length; i++) 
			{
				_nexts.push(arg[i]);
			}
		}
		
		public function get display():DisplayObject
		{
			return _display;
		}

		public function addAnt(ant:Ant):void{	
			if(ant.power==_power){
				antCount++;
			}else{
				_isAttack=true;
				antCount--;
				
				if(antCount<=0){
					antCount=0;
				    setPower(ant.power);
					_isNeutral=ant.home.isNeutral;
					autoGrow=ant.home.autoGrow;
					_isAttack=false;
				}
				
			}
			ant.release();
			//setCount(antCount);
		}
		
		/**
		 * 是否相邻 
		 * @return 
		 */
		public function isBorder(ant:Castle):Boolean{
			return _nexts.indexOf(ant)>=0;
		}

		public function createAntTo(target:Castle):void{  
			if(this==target.toTarget) return;
			_creating=true;
			toTarget=target;
		}
		
		private function createAnt():void{
			if(!_creating) return;
			if(antCount<=1){
				return ;
			}

			antCount--;
			var ant:Ant =  Ant(Ant.antPool.getObject()).reset(_power);
			ant._speed=_speed;
			ant._blood=10;
			ant.setHome(this);
			ant.reset(_power);
			_scene.addAnt(ant);
			ant.x=x;
			ant.y=y;
			
			ant.moveTo(toTarget);
			if(antCount<=least  || toTarget==null){
				_creating=false;
				toTarget=null ;
			}
		}
	}
}